package com.unicdata.ai.demo_2af.common.config;

import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorConfig;
import com.warrenstrange.googleauth.KeyRepresentation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 双因子验证
 *
 * @author lhn
 * @date 2025/06/26 16:41
 * @since 1.0.0
 **/
@Configuration
public class TwoFactorConfig {

    @Bean
    public GoogleAuthenticator googleAuthenticator() {
        GoogleAuthenticatorConfig config = new GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder()
                .setTimeStepSizeInMillis(TimeUnit.SECONDS.toMillis(30))
                .setWindowSize(1) // 允许前后1个计数的验证码，即±30秒
                .setCodeDigits(6) // 验证码长度
                .setKeyRepresentation(KeyRepresentation.BASE32) // 密钥表示方式
                .build();
        return new GoogleAuthenticator(config); // 直接在构造函数中传入配置
    }

}